package com.cmc.cloud.cmclink.doc.vo.manifestvo.manifestcntr;

import com.alibaba.excel.annotation.ExcelProperty;
import com.cmc.cloud.cmclink.doc.api.document.rep.manifestrep.manifestcntr.ManifestCntrBaseVO;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author 陈正
 * @date 2025/8/26
 */
@Schema(description = "管理后台 - 舱单箱信息 导入excel VO")
@Data
@ToString(callSuper = true)
@AllArgsConstructor
@NoArgsConstructor
public class ManifestCntrImportVo extends ManifestCntrBaseVO {
    /**
     * 用名字去匹配，这里需要注意，如果名字重复，会导致只有一个字段读取到数据
     */
    @ExcelProperty(value = "船名")
    private String bookingVesselCn;
    @ExcelProperty(value = "航次")
    private String bookingVoyageCn;
    @ExcelProperty(value = "提单号")
    private String manifestNoCn;
    @ExcelProperty(value = "箱号")
    private String cntrNoCn;
    @ExcelProperty(value = "箱型")
    private String cntrSizeTypeCn;
    @ExcelProperty(value = "封号")
    private String sealNoCn;
    @ExcelProperty(value = "件数")
    private String quantityCn;
    @ExcelProperty(value = "重量")
    private String grossWeightCn;
    @ExcelProperty(value = "尺码")
    private String measurementCn;
    @ExcelProperty(value = "整拼")
    private String lclCn;
    @ExcelProperty(value = "BLNO")
    private String manifestNoEn;
    @ExcelProperty(value = "CONTAINERN")
    private String cntrNoEn;
    @ExcelProperty(value = "SEALNO")
    private String sealNoEn;
    @ExcelProperty(value = "SIZE")
    private String cntrSizeEn;
    @ExcelProperty(value = "TYPE")
    private String cntrTypeEn;
    @ExcelProperty(value = "QUANTITY")
    private String quantityEn;
    @ExcelProperty(value = "WEIGHT")
    private String grossWeightEn;
    @ExcelProperty(value = "MEASURE")
    private String measurementEn;
    @ExcelProperty(value = "FULL")
    private String lclEn;
    /**
     * 错误信息
     */
    private List<String> errorMsgList = new ArrayList<>();
    private static List<String> lcl_type_list = Arrays.asList("F", "S");

    public void checkNullData(boolean importTypeCn) {
        if (importTypeCn) {
            /**
             * 检查中文导入
             */
            if (StringUtils.isEmpty(bookingVesselCn)) {
                errorMsgList.add("船名不能为空");
            }
            if (StringUtils.isEmpty(bookingVoyageCn)) {
                errorMsgList.add("航次不能为空");
            }
            if (StringUtils.isEmpty(manifestNoCn)) {
                errorMsgList.add("提单号不能为空");
            } else {
                setManifestNo(manifestNoCn);
            }
            if (StringUtils.isEmpty(cntrNoCn)) {
                errorMsgList.add("箱号不能为空");
            } else {
                setCntrNo(cntrNoCn);
            }
            if (StringUtils.isEmpty(cntrSizeTypeCn)) {
                errorMsgList.add("箱型不能为空");
            } else {
                try {
                    setCntrSize(cntrSizeTypeCn.substring(0, 2));
                    setCntrType(cntrSizeTypeCn.substring(2, cntrSizeTypeCn.length()));
                } catch (Exception e) {
                    errorMsgList.add(cntrSizeTypeCn + "箱型格式错误");
                }
            }
            if (StringUtils.isEmpty(sealNoCn)) {
                errorMsgList.add("封号不能为空");
            } else {
                setSealNo(sealNoCn);
            }
            if (StringUtils.isEmpty(quantityCn)) {
                errorMsgList.add("件数不能为空");
            } else {
                try {
                    setQuantity(Long.parseLong(quantityCn));
                    if (getQuantity() <= 0) {
                        errorMsgList.add(quantityCn + "件数必须为正整数");
                    }
                } catch (Exception e) {
                    errorMsgList.add(quantityCn + "件数必须为整数");
                }
            }
            if (StringUtils.isEmpty(grossWeightCn)) {
                errorMsgList.add("重量不能为空");
            } else {
                try {
                    setGrossWeight(new BigDecimal(grossWeightCn));
                    if (getGrossWeight().compareTo(BigDecimal.ZERO) <= 0) {
                        errorMsgList.add(grossWeightCn + "重量必须为正整数");
                    }
                } catch (Exception e) {
                    errorMsgList.add(grossWeightCn + "重量必须为整数");
                }
            }
            if (StringUtils.isEmpty(measurementCn)) {
                errorMsgList.add("尺码不能为空");
            } else {
                try {
                    setMeasurement(new BigDecimal(measurementCn));
                    if (getMeasurement().compareTo(BigDecimal.ZERO) <= 0) {
                        errorMsgList.add(measurementCn + "尺码必须正整数");
                    }
                } catch (Exception e) {
                    errorMsgList.add(measurementCn + "尺码必须为整数");
                }
            }
        } else {
            /**
             * 检查英文导入
             */
            if (StringUtils.isEmpty(manifestNoEn)) {
                errorMsgList.add("BLNO不能为空");
            } else {
                setManifestNo(manifestNoEn);
            }
            if (StringUtils.isEmpty(cntrNoEn)) {
                errorMsgList.add("CONTAINERN不能为空");
            } else {
                setCntrNo(cntrNoEn);
            }
            if (StringUtils.isEmpty(sealNoEn)) {
                errorMsgList.add("SEALNO不能为空");
            } else {
                setSealNo(sealNoEn);
            }
            if (StringUtils.isEmpty(cntrSizeEn)) {
                errorMsgList.add("SIZE不能为空");
            } else {
                setCntrSize(cntrSizeEn);
                try {
                    int cntrSizeTemp = (Integer.parseInt(cntrSizeEn));
                    if (cntrSizeTemp <= 0) {
                        errorMsgList.add(cntrSizeEn + "SIZE必须为正整数");
                    }
                } catch (Exception e) {
                    errorMsgList.add(cntrSizeEn + "SIZE必须为整数");
                }
            }
            if (StringUtils.isEmpty(cntrTypeEn)) {
                errorMsgList.add("TYPE不能为空");
            } else {
                setCntrType(cntrTypeEn);
            }
            if (StringUtils.isEmpty(quantityEn)) {
                errorMsgList.add("QUANTITY不能为空");
            } else {
                try {
                    setQuantity(Long.parseLong(quantityEn));
                    if (getQuantity() <= 0) {
                        errorMsgList.add(quantityEn + "QUANTITY必须为正整数");
                    }
                } catch (Exception e) {
                    errorMsgList.add(quantityEn + "QUANTITY必须为整数");
                }
            }
            if (StringUtils.isEmpty(grossWeightEn)) {
                errorMsgList.add("WEIGHT不能为空");
            } else {
                try {
                    setGrossWeight(new BigDecimal(grossWeightEn));
                    if (getGrossWeight().compareTo(BigDecimal.ZERO) <= 0) {
                        errorMsgList.add(grossWeightEn + "WEIGHT必须为正整数");
                    }
                } catch (Exception e) {
                    errorMsgList.add(grossWeightEn + "WEIGHT必须为整数");
                }
            }
            if (StringUtils.isEmpty(measurementEn)) {
                errorMsgList.add("MEASURE不能为空");
            } else {
                try {
                    setMeasurement(new BigDecimal(measurementEn));
                    if (getMeasurement().compareTo(BigDecimal.ZERO) <= 0) {
                        errorMsgList.add(measurementEn + "MEASURE必须正整数");
                    }
                } catch (Exception e) {
                    errorMsgList.add(measurementEn + "MEASURE必须为整数");
                }
            }
        }
    }
}
